package com.cai.kcsj.controller;

import com.cai.kcsj.entity.Goods;
import com.cai.kcsj.entity.Type;
import com.cai.kcsj.service.GoodsServer;
import com.cai.kcsj.service.TypeServer;
import com.cai.kcsj.utils.DateUtil;
import com.cai.kcsj.utils.Uploadpath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/4 18 : 31
 * @package：com.cai.kcsj.controller
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsServer goodsServer;

    @Autowired
    private TypeServer typeServer;

    @RequestMapping("/allgoods")
    private String selectAllGoods(Model model, @RequestParam(defaultValue = "1") Integer current){
        int startpage = (current-1)*5;
        int count = goodsServer.getGoodsCount();
        int page = count%5==0?count/5:count/5+1;
        List<Goods> goods = goodsServer.selectGoodsList(startpage);
        model.addAttribute("goodsList",goods);
        model.addAttribute("page",page);
        model.addAttribute("current",current);
        return "goodsmanager";
    }

    @RequestMapping("/goodsup")
    public String goodsup(MultipartFile mfile, Goods goods){
        if(!mfile.isEmpty()){
            String originalFilename = mfile.getOriginalFilename();
            String extensionName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = new Date().getTime() + extensionName;
//        String path = "E:\\Intellij\\kcsj\\src\\main\\resources\\static\\img";
            String uploadPath = Uploadpath.getUploadPath();
            System.out.println("uploadPath:: "+uploadPath);
            try {

                mfile.transferTo(new File(uploadPath, newFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }

            goods.setImg(newFileName);
        }else{
            goods.setImg("1.jpg");
        }

        goods.setUptime(DateUtil.getDate());
        goodsServer.insertGoods(goods);
        return "redirect:/goods/allgoods";
    }

    @RequestMapping("/updateGoods/{id}")
    public String updateGoods(Model model,@PathVariable("id") Integer id){
        Goods goods = goodsServer.selectGoodsById(id);
        List<Type> types = typeServer.selectType();
        model.addAttribute("goods",goods);
        model.addAttribute("typeList",types);
        return "goodsEdit";
    }

    @RequestMapping("/update")
    public String updateGoods(MultipartFile mfile, Goods goods){

        if(!mfile.isEmpty()){
            String originalFilename = mfile.getOriginalFilename();
            String extensionName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = new Date().getTime() + extensionName;
//        String path = "E:\\Intellij\\kcsj\\src\\main\\resources\\static\\img";
            String uploadPath = Uploadpath.getUploadPath();
            try {
                mfile.transferTo(new File(uploadPath, newFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            goods.setImg(newFileName);
        }
        goods.setUptime(DateUtil.getDate());
        goodsServer.updateGoods(goods);
        return "redirect:/goods/allgoods";
    }
}
